package cn.xzq.crm.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.xzq.crm.entity.Customer;

@Repository("customerDaoImpl")
public class CustomerDaoImpl implements CustomerDao {
	
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	/**
	 * 添加一个客户
	 * @param customer
	 */
	@Override
	public void add(Customer customer) {
		hibernateTemplate.save(customer);
	}

	/**
	 * 查找所有客户
	 */
	public List<Customer> findAll() {
		String hql = "from Customer";
		List<Customer> list = (List<Customer>) hibernateTemplate.find(hql);
		return list;
	}

	/**
	 * 删除一个客户
	 */
	public void delete(Customer customer) {
		hibernateTemplate.delete(customer);
	}

	/**
	 * 根据id查找一个客户
	 * @param custId
	 */
	public Customer findOneById(String custId) {
		return hibernateTemplate.get(Customer.class, custId);
	}

	/**
	 * 更新用户信息
	 * @param custId
	 */
	public void update(Customer customer) {
		hibernateTemplate.update(customer);
	}
	
	/**
	 * 查找分页数据
	 * @param begin
	 * @param pageSize
	 * @return
	 */
	public List<Customer> findbyPageBean(int begin, int pageSize){
		/*//使用hibernate底层代码
		//得到sessionFactory
		SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		//设置分页信息
		Query query = session.createQuery("from Customer");
		query.setFirstResult(begin);
		query.setMaxResults(pageSize);
		List<Customer> alist = query.list();*/
		
		//使用hibernate离线对象
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		List<Customer> list = (List<Customer>) hibernateTemplate.findByCriteria(criteria, begin, pageSize);
		return list;
	}
	
	/**
	 * 查找记录数
	 * @return
	 */
	@SuppressWarnings("all")
	public int findAllCount(){
		String hql = "select count(*) from Customer";
		List<Object> list = (List<Object>) hibernateTemplate.find(hql);		
		if(list != null && list.size() != 0){
			Object obj = list.get(0);
			//变成int类型
			Long l_obj = (long)obj;
			int count = l_obj.intValue();
			return count;
		}
		return 0;
	}
	
	/**
	 * 条件查询
	 * @param customer
	 * @return
	 */
	@SuppressWarnings("all")
	public List<Customer> findAllByCondition(Customer customer){
		//三种方式
		//第一种
		/*SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
		Session session = sessionFactory.getCurrentSession();
		//设置分页信息
		Query query = session.createQuery("from Customer where custName=?");
		query.setParameter(0, "%"+customer.getCustName()+"%");
		List<Customer> alist = query.list();*/
		
		//第二种(单条件-建议使用)
		/*List<Customer> list = (List<Customer>) hibernateTemplate.find("from Customer custName like ?",
				"%"+customer.getCustName()+"%");
		return list;*/
		
		//第三种(离线对象，多条件--推荐使用)
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		criteria.add(Restrictions.like("custName", "%"+customer.getCustName()+"%"));
		List<Customer> list = (List<Customer>) hibernateTemplate.findByCriteria(criteria);
		return list;
	}
	
	/**
	 * 多条件组合查询
	 * @param customer
	 * @return
	 */
	public List<Customer> findMoreCondition(Customer customer){
		//使用hibernateTamplate，find完成查询
/*		StringBuilder hql = new StringBuilder("from Customer where 1=1");
		//定义一个list集合存放查询的参数
		List<Object> params = new ArrayList<Object>();
		//判断查询条件，存在则加到查询语句中
		if(customer.getCustName()!=null && !customer.getCustName().isEmpty()){
			hql.append(" and custName=?");
			params.add(customer.getCustName());
		}
		if(customer.getCustLevel()!=null && !customer.getCustLevel().isEmpty()){
			hql.append(" and custLevel=?");
			params.add(customer.getCustLevel());
		}
		if(customer.getCustSource()!=null && !customer.getCustSource().isEmpty()){
			hql.append(" and custSource=?");
			params.add(customer.getCustSource());
		}
		if(customer.getCustIndustry()!=null && !customer.getCustIndustry().isEmpty()){
			hql.append(" and custIndustry=?");
			params.add(customer.getCustIndustry());
		}
		if(customer.getCustAddress()!=null && !customer.getCustAddress().isEmpty()){
			hql.append(" and custAddress=?");
			params.add(customer.getCustAddress());
		}
		if(customer.getCustPhone()!=null && !customer.getCustPhone().isEmpty()){
			hql.append(" and custPhone=?");
			params.add(customer.getCustPhone());
		}
		System.out.println(hql.toString());
		System.out.println( params.toArray());
		List<Customer> list = (List<Customer>) hibernateTemplate.find(hql.toString(), params.toArray());*/
		
		//使用离线对象完成多条件组合查询
		
		//创建离线对象
		DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
		//判断查询条件，存在则加到查询语句中
		if(customer.getCustName()!=null && !customer.getCustName().isEmpty()){
			criteria.add(Restrictions.like("custName", "%"+customer.getCustName()+"%"));
		}
		if(customer.getCustLevel()!=null && !customer.getCustLevel().isEmpty()){
			criteria.add(Restrictions.like("custLevel", "%"+customer.getCustLevel()+"%"));
		}
		if(customer.getCustSource()!=null && !customer.getCustSource().isEmpty()){
			criteria.add(Restrictions.like("custSource", "%"+customer.getCustSource()+"%"));
		}
		if(customer.getCustIndustry()!=null && !customer.getCustIndustry().isEmpty()){
			criteria.add(Restrictions.like("custIndustry", "%"+customer.getCustIndustry()+"%"));
		}
		if(customer.getCustAddress()!=null && !customer.getCustAddress().isEmpty()){
			criteria.add(Restrictions.like("custAddress", "%"+customer.getCustAddress()+"%"));
		}
		if(customer.getCustPhone()!=null && !customer.getCustPhone().isEmpty()){
			criteria.add(Restrictions.like("custPhone", "%"+customer.getCustPhone()+"%"));
		}
		List<Customer> list = (List<Customer>) hibernateTemplate.findByCriteria(criteria);
		return list;
	}
	
	
}
